package gwt.bbsbot.server;

import gwt.bbsbot.server.datastore.DataStoreUtil;
import gwt.bbsbot.server.twitter.TwitterManager;
import gwt.bbsbot.server.twitter.beans.TweetBean;
import gwt.bbsbot.server.twitter.constant.TwitterConstant;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InsertServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		//テストBOT
		TwitterManager tm = new TwitterManager(TwitterConstant.ID_TEST, TwitterConstant.PASS_TEST);
		List<TweetBean> list = tm.search();
		
		//つぶやきを1件ずつ保存する
		for(TweetBean bean:list){
			//保存済みのつぶやきか確認する
			if(!DataStoreUtil.isExistQuestionData(bean)){
				//新規ならスレを立てる
				DataStoreUtil.insertQuestionDataByTweetBean(bean);
			}
		}

		//JAVABOT
		//PythonBOT
		
		//レスポンス情報を作成する
		resp.setContentType("text/html; charset=\"utf-8\"");
		PrintWriter out = resp.getWriter();
		out.write("<html>");
		out.write("<head><title>AndroindGPS</title></head>");
		out.write("<body>");
		out.write("<h2> insert ok </h2>");
		out.write("</body>");
		out.write("</html>");	
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}
